<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use think\Session;
use think\helper\Time;
class Formmerchant extends Form
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'weiui';
    protected $model = null;
    protected $mermodel = null;
    public function _initialize()
    {

        parent::_initialize();
        $this->model = new \app\admin\model\FormMerchant;
        $this->mermodel = new \app\admin\model\Merchant;


    }
    public function index()
    {
        $title = '教职工、校内商户防疫信息摸底统计';
        // $openid = 'oMYPNwg_cHGJQh6vebU-FtZf04BM';
        $openid = Session::get('wechatUser')['openid'];
        $row = $this->model->where(['openid'=>$openid])->find();
        if($row)
            $title = '教职工、校内商户防疫信息日报统计';
        $this -> assign('pageTitle',$title);
        $this -> assign('title',$title);
        $this -> assign('wxcode','merchant');
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            $where= [
                'nickname' => $params['nickname'],
                'tel' => $params['tel']
            ];
            $ret = $this->mermodel->where($where)->find();

            if(!$ret){
                $this->error('姓名、手机号填写错误', null,1);
            }

            if(isset($params['abc']) && $params['abc'] && isset($params['longitude']) && $params['longitude']){
                $address = getAddress($params['longitude'],$params['abc']);

                if($address){
                    $params['formatted_address'] = $address['formatted_address'];
                    $params['city'] = $address['city'];
                    $params['province'] = $address['province'];
                    $params['district'] = $address['district'];
                    $params['street'] = $address['street'];
                }
            }

            $today = Time::today();
            $today_row = $this->model->where(['openid'=>$openid])->whereTime('create_time','between',$today)->find();
            if($today_row)
            {
                $res = $today_row->allowField(true)->save($params);
                $this->success('上报成功','/merchant',1);
            }
            else
            {
                $params['openid'] = $openid;
                $res = $this->model->allowField(true)->save($params);
                if ($res)
                    $this->success('上报成功','/merchant',1);
                else
                    $this->error('上报失败','/merchant',1);
            }
            // $house=$this->model->where(['user_id'=>$this->id,''])->find()
            // print_r($data);
        }
        $wxjs = $this -> app->jssdk->buildConfig(array('getLocation'), false);
        $this -> assign('wxjs',$wxjs);
        return $this->view->fetch();
    }

}
